package com.yanqu.road.server.cmd.activity.daomu.pvp;

import com.yanqu.road.entity.activity.daomu.config.DaoMuActivityConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.DaoMuPvpProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.daomu.DaoMuActivity;
import com.yanqu.road.server.manager.activity.daomu.DaoMuActivityMgr;
import com.yanqu.road.server.manager.activity.daomu.pvp.DaoMuActivityPvp;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;


@Cmd(code = Protocol.C_CROSS_DAOMU_PVP_ENTER_ACTIVITY, desc = "进入pvp活动")
public class DaoMuPvpEnterActivityCmd extends CrossCommand {

    private int activityId;

    private long playerId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        DaoMuPvpProto.DaoMuPvpEnterActivityReqMsg reqMsg = DaoMuPvpProto.DaoMuPvpEnterActivityReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        playerId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        DaoMuActivity activity = DaoMuActivityMgr.getActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST;
            return;
        }
        DaoMuActivityPvp activityPvp = activity.getDaoMuActivityPvp();
        if (activityPvp == null) {
            ret = GameErrorCode.E_DAOMU_PVP_NOT_OPEN;
            return;
        }
        DaoMuActivityConfig config = activityPvp.getConfig();
        if (config == null) {
            ret = GameErrorCode.E_DAOMU_PVP_CONFIG_NOT_FOUND;
            return;
        }
        // 把属性带进pvp更新下
        ret = activityPvp.enterActivityByCmd(playerId);

    }

    @Override
    public void sendData() {
        if (ret != 0) {
            DaoMuPvpProto.DaoMuPvpEnterActivityRespMsg.Builder respMsg = DaoMuPvpProto.DaoMuPvpEnterActivityRespMsg.newBuilder();
            respMsg.setRet(ret);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_DAOMU_PVP_ENTER_ACTIVITY, respMsg));
        }
    }
}
